
package net.atremmedia.gwt.actionjava.examples.bounds.samples;

import net.atremmedia.gwt.actionjava.examples.bounds.samples.curve.DrawCurve;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Graphics;
import actionjava.geom.Point2D;
import actionjava.geom.curve.ArcCurve2D;

public class DrawArcBounds2 extends DrawCurve
{
	public static final String NAME = "Draw arc bounds 2";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-bounds/src/main/java/net/atremmedia/gwt/actionjava/examples/bounds/samples/DrawArcBounds2.java";

	private Point2D p1;
	private Point2D p2;
	private Point2D p3;
	private double startAngle;
	private double extentAngle;
	private double endAngle;
	private int radius;

	/*
	 * ---------------------------------------------------------------
	 *	Constructor & init
	 * ---------------------------------------------------------------
	 */

	public DrawArcBounds2(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	private void initialize()
	{
		p1 = new Point2D(getRandomNumber(200, getWidth() - 200), getRandomNumber(200, getHeight() - 200));

		startAngle = getRandomNumber(-360, 360);
		extentAngle = getRandomNumber(-360, 360);
		endAngle = extentAngle + startAngle;
		radius = ((Double) getRandomNumber(25, 100)).intValue();
	}

	/*
	 * ---------------------------------------------------------------
	 *	Abstract Overrides
	 * ---------------------------------------------------------------
	 */

	@Override
	public void run()
	{
		initialize();
		super.run(true);

		addChild(getCurveShape());
	}

	@Override
	public void drawCurve(Graphics graph)
	{
		// Arc curve line
		graph.beginStroke(CURVE_LINE_COLOR);
		graph.arc(p1.x, p1.y, radius, Math.toRadians(startAngle), Math.toRadians(endAngle), (extentAngle < 0) ? true : false);
		graph.endStroke();
	}

	@Override
	public void drawDots(Graphics graph)
	{
		// Start point
		drawDot(p2.x, p2.y, P1_DOT_COLOR, graph);

		// End point
		drawDot(p3.x, p3.y, P2_DOT_COLOR, graph);
	}

	@Override
	public void drawControlPointLines(Graphics graph)
	{
	}

	@Override
	public void setBounds()
	{
		// Arc curve bounds (Rectangle)
		ArcCurve2D arcCurve2D = new ArcCurve2D(p1.x, p1.y, radius, Math.toRadians(startAngle), Math.toRadians(endAngle), ((extentAngle < 0) ? true : false));
		p2 = arcCurve2D.getArcStartPoint();
		p3 = arcCurve2D.getArcEndPoint();

		super.setBounds(arcCurve2D.getBounds());
	}
}
